/*
 * Copyright (c) 2009-2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "alkes/sys/win32/MemoryMapImpl_win32.h"
#include "alkes/core/Memory.h"

#if (ALKES_API_WIN32 == 1)

namespace alkes {

MemoryMapImpl::MemoryMapImpl(const char* filepath)
: hfile_(INVALID_HANDLE_VALUE)
, hmapped_(NULL)
, address_(NULL)
{
    filepath_ = (const char*)_strdup(filepath);
}

MemoryMapImpl::~MemoryMapImpl()
{
    if (filepath_)
        alkes_free((void*)filepath_);
}

bool MemoryMapImpl::open()
{
    if (INVALID_HANDLE_VALUE != hfile_)
        return false;

    hfile_ = ::CreateFile(
        filepath_,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL);

    if (INVALID_HANDLE_VALUE == hfile_)
        return false;

    hmapped_ = ::CreateFileMapping(
        hfile_, NULL,
        PAGE_READONLY,
        0,0,
        NULL);

    if (NULL == hmapped_)
    {
        ::CloseHandle(hfile_);
        hfile_ = INVALID_HANDLE_VALUE;
    }

    return NULL != hmapped_;
}

void MemoryMapImpl::close()
{
    if (NULL != address_)
    {
        ::UnmapViewOfFile(address_);
        address_ = NULL;
    }

    if (NULL != hmapped_)
    {
        ::CloseHandle(hmapped_);
        hmapped_ = NULL;
    }

    if (INVALID_HANDLE_VALUE != hfile_)
    {
        ::CloseHandle(hfile_);
        hfile_ = INVALID_HANDLE_VALUE;
    }
}

int32_t MemoryMapImpl::size()
{
    if (INVALID_HANDLE_VALUE == hfile_)
        return -1;

    DWORD low;

    low = ::GetFileSize(hfile_, NULL);

    return low == -1 ? -1 : (int32_t)low;
}

void* MemoryMapImpl::lock(int32_t* size)
{
    if (NULL != hmapped_)
        return 0;

    if (address_)
        return 0;

    address_ = ::MapViewOfFile(
        hmapped_,
        FILE_MAP_READ,
        0,
        0,
        0);

    if (address_ && size)
        *size = this->size();

    return address_;
}

void MemoryMapImpl::unlock(void* address)
{
    if (address_)
    {
        ::UnmapViewOfFile(address);
        address_ = NULL;
    }
}

}

#endif
